/**
 * 宽带预约列表
 */
(function($) {
  var pageData = {};
  var pageCount = 1;
  var filterType = 3;
  var pageNumber = 0;
  var groupList = new Array();  
  /*
  封装参数
  */
  var getMyParams = function(group_no,group_name) {
    var params = "?";
    var myParam = getParams();
    if (myParam) {
      params += myParam;
      if (group_no) {
        params += "&";
      }
    }
    if (group_no) {
      params += "group_no=" + group_no;
    }
    if(group_name){
      params += "&group_name="+group_name;
    }

    return params;
  };

  var bind = function(object, clickFunc) {
    itemOnClick(object, clickFunc);
  }

  var itemOnClick = function(object, clickFunc) {
    $(object).off("click"); //取消li事件
    /*li事件*/
    $(object).on("click", function() {
      var $siblingsNode = $(this).siblings();
      var primaryClass = "select-item";
      var defaultColor = "#fff";
      var selectColor = "#0c79b1";

      if ($siblingsNode.hasClass(primaryClass)) {
        $siblingsNode.removeClass(primaryClass);
        $siblingsNode.css("background-color", defaultColor);
      }
      if ($(this).hasClass(primaryClass)) {
        $(this).removeClass(primaryClass);
        $(this).css("background-color", defaultColor);
      } else {
        $(this).addClass(primaryClass);
        $(this).css("background-color", selectColor);
      }
      clickFunc(this);
      event.stopPropagation();
    });
  }


  var groupClickFunc = function(object) {
    var groupID = $(object).find("input[name='group_id']").val();
    var groupName = $(object).find("div[name='group_name']").html();
    $("#group_id").val(groupID);
    $("#group_name").val(groupName);
    $('#my-alert').modal('close');
    window.location.href = "opponent_list.html" +  getMyParams(groupID,groupName);
  }

  /*
  显示数据
  */
  var showGroupData = function(data) {
    if (data.groupInfoList instanceof Array) {
      list = data.groupInfoList;
      for (var i = 0; i < list.length; i++) {
        groupList.push(list[i]);
      }
    } else if (data.groupInfoList instanceof Object) {
      if(data.groupInfoList.empty == undefined){
        groupList.push(data.groupInfoList);
      }
    }
    var $tpl = $('#amz-group-list');
    var source = $tpl.text();
    var template = Handlebars.compile(source);
    var myData = {
      list: groupList,
      "error": "没有集团信息"
    };

    $("#group-list").children('li').remove();
    $("#group-list").children('div').remove();

    var html = template(myData);
    $tpl.before(html);
    bind("#group-list > li", groupClickFunc);
  };

  var isDigital = function(keyword) {
    var reg = /^[0-9]*$/;
    if (reg.test(keyword)) {
      return true;
    }
    return false;
  }

  var buildQueryGroupParams = function(pageNumber, keyword) {
    var url = "grpName=";    
    if (isDigital(keyword)) {
      url = url + "&grpNo=" + keyword + "&serviceNo=&pageStart=" + pageNumber*30 + "&pageLimit=30";
    } else {
      url = url + encodeURI(keyword) + "&grpNo=" + "&serviceNo=&pageStart=" + pageNumber*30 + "&pageLimit=30";
    }

    params = {
      "token": getParamValue("token"),
      "staffNo": getParamValue("EmpCode"),
      "passUrl": url
    };
    console.log("request:" + JSON.stringify(params));
    return params;
  }

  var loadGroupData = function(pageNumber) {
    showLoadingBar($("#loadingbar"), true);
    var keyword = $("#keyword").val();
    params = buildQueryGroupParams(pageNumber, keyword);

    $.ajax({
      type: "post",
      contentType: "application/json",
      data: JSON.stringify(params),
      url: Constants.rpcEsopHost + "queryGrouInfo",
      dataType: "json",
      async: true,
      success: function(data) {
        showLoadingBar($("#loadingbar"), false);
        console.log("data = " + data);
        pageData = data.return;
        console.info(data.return);
        if (data.rtnCode != 1000) {
          // showBottomMsg(data.rtnMsg);
          console.log("获取RPC接口返回失败！code:" + data.rtnCode);
          return;
        }
        if (data.return.resultCode != "00") {
          console.log("获取信息接口返回失败！code:" + data.return.resultCode);
          return;
        }
        showGroupData(data.return);
      },
      error: function(textStatus) {
        showLoadingBar($('#loadingbar'), false);;
        console.info("request error:");
      }
    });
  };

  $(function() {
    /*滚动条*/
    var $doc = $(document);
    $doc.scroll(function() {
      console.log("height = " + ($doc.height() - $(window).height()));
      console.log("scrollTop = " + Math.ceil($doc.scrollTop()));
      console.log("Math.ceil($doc.scrollTop()) = " + Math.ceil($doc.scrollTop()));
      if (Math.ceil($doc.scrollTop()) >= ($doc.height() - $(window).height())) {
        var $msg = $("#no-data-msg");
        console.log("debug 1");
        if (!$.isEmptyObject(pageData)) {
          console.log("debug 2");
          if (!$msg.hasClass("am-hide")) {
            console.log("debug 3");
            $msg.addClass("am-hide");
          }
          console.log("pageNumber="+pageNumber);
          pageNumber ++;
          loadGroupData(pageNumber);
        } else {
          console.log("debug 5");
          if ($msg.hasClass("am-hide")) {
            console.log("debug 6");
            $msg.removeClass("am-hide");
          }
        }
      }
    });

    $("#search_button").on("click", function() {
      groupList = [];
      loadGroupData(pageNumber);
    });
  });
})(jQuery);